﻿using SuperX.Common;
using SuperX.Common.Object;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace SuperX.DataService
{
    [StructLayout(LayoutKind.Sequential)]
    public struct Scaling : IComparable<Scaling>
    {
        public long ID;

        public ScaleType ScaleType;

        public float EUHi;

        public float EULo;

        public float RawHi;

        public float RawLo;

        public Scaling(long id, ScaleType type, float euHi, float euLo, float rawHi, float rawLo)
        {
            ID = id;
            ScaleType = type;
            EUHi = euHi;
            EULo = euLo;
            RawHi = rawHi;
            RawLo = rawLo;
        }

        public int CompareTo(Scaling other)
        {
            return ID.CompareTo(other.ID);
        }

        public static readonly Scaling Empty = new Scaling { ScaleType = ScaleType.None };
    }

    public struct ItemData<T>
    {
        public T Value;
        public long TimeStamp;
        public QUALITIES Quality;

        public ItemData(T value, long timeStamp, QUALITIES quality)
        {
            Value = value;
            TimeStamp = timeStamp;
            Quality = quality;
        }
    }

    public enum ScaleType : byte
    {
        None = 0,
        Linear = 1,
        SquareRoot = 2
    }

    public class TagConfig : BasicObject
    {
        public const string Defalut = "Default";
        public const string WorkState = "WorkState";
        public const string Online = "Online";
        public const string AlarmState = "AlarmState";

        /// <summary>
        /// 寄存器地址
        /// </summary>
        public string Regester { get; set; }

        /// <summary>
        /// 编号
        /// </summary>
        public string No { get; set; }

        /// <summary>
        /// 寄存器地址+编号
        /// </summary>
        public string Address { get; set; }

        public DataType DataType { get; set; }

        public ushort Size { get; set; }

        /// <summary>
        /// 是否归档
        /// </summary>
        public bool HDASave { get; set; }

        public long Maximum { get; set; }

        public long Minimum { get; set; }

        public bool Archive { get; set; }

        public long ID { get; set; }

        public int Cycle { get; set; }

        /// <summary>
        /// 时间触发
        /// </summary>
        public bool TimeTrigger { get; set; }

        /// <summary>
        /// 定时刷新间隔
        /// </summary>
        public double Interval { get; set; }

        public ReadWriteMode ReadWriteMode { get; set; }

        /// <summary>
        /// 表达式脚本
        /// </summary>
        public string Script { get; set; }

        /// <summary>
        /// 变量类型
        /// </summary>
        public TagType TagType { get; set; }

        /// <summary>
        /// 比例
        /// </summary>
        public float ScaleValue { get; set; }

        /// <summary>
        /// 单位
        /// </summary>
        public string Unit { get; set; }

        public TagConvertType ConverterType { get; set; }
    }
}